﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PCMSofCC.BLL;

namespace PCMSofCC.Admin
{
    public partial class AssignSiteStep1 : System.Web.UI.Page
    {

        List<DateTime> availableDays;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["Days"]==null)
            {
                availableDays = new List<DateTime>();
            }
            else
            {
                availableDays = (List<DateTime>)ViewState["Days"];
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (availableDays.Contains(e.Day.Date))
                e.Day.IsSelectable = true;
            else
                e.Day.IsSelectable = false;
        }

        protected void lbxUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime startTime = DateHelper.GetMondayOfThisWeek(DateTime.Now.Date).AddDays(7);
            DateTime endTime = startTime.AddDays(6);
            availableDays.Clear();
            Calendar1.SelectedDate = DateTime.MinValue;
            int? flags;
            DateTime? submitDate;
            Guid userId = new Guid(lbxUsers.SelectedValue);
            AvailableTime.GetAvailableDayAndSubmitDate(userId, out flags, out submitDate);
            if (submitDate != null)
                if (submitDate.Value.AddDays(7) >= startTime && submitDate.Value.AddDays(7) <= endTime)
                {
                    litSubmitDate.Text = submitDate.Value.ToShortDateString() + "  按时提交";
                    WeekDay flag = (WeekDay)flags;
                    WeekDay test;
                    if ((test = flag & WeekDay.Monday) == WeekDay.Monday)
                        availableDays.Add(startTime.AddDays(0));
                    if ((test = flag & WeekDay.Tuesday) == WeekDay.Tuesday)
                        availableDays.Add(startTime.AddDays(1));
                    if ((test = flag & WeekDay.Wednesday) == WeekDay.Wednesday)
                        availableDays.Add(startTime.AddDays(2));
                    if ((test = flag & WeekDay.Thursday) == WeekDay.Thursday)
                        availableDays.Add(startTime.AddDays(3));
                    if ((test = flag & WeekDay.Friday) == WeekDay.Friday)
                        availableDays.Add(startTime.AddDays(4));
                    if ((test = flag & WeekDay.SaturDay) == WeekDay.SaturDay)
                        availableDays.Add(startTime.AddDays(5));
                    if ((test = flag & WeekDay.Sunday) == WeekDay.Sunday)
                        availableDays.Add(startTime.AddDays(6));
                }
                else
                    litSubmitDate.Text = submitDate.Value.ToShortDateString() + "  未按时提交";
            else
                litSubmitDate.Text = "未提交";
            ViewState["Days"] = availableDays;
            btnSelect.Enabled = false;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            btnSelect.Enabled = true;
        }
    }
}
